#!/bin/bash
#: Title		: Safe Mount
#: Date			: 05/15/2011
#: Author		: "John Lehr" <slo.sleuth@gmail.com>
#: Version		: 1.0.1
#: Description	: simplified mounting script for forensic analysis

#: 05/15/2011	: v1.0.1 Bug fix, print_deviceinfo made Ubuntu compatible
#: 05/13/2011	: v1.0.0 First release
#: 05/12/2011	: v0.1.0 gui framework established

## To do:


## Variables
TITLE="Safe Mount"
IMAGE="gtk-harddisk"

## Functions
check_mount ()
{
	if [ $EXIT_CODE = 0 ]; then
		mount_success
		exit 0
	else
		mount_fail
		exit 1
	fi
}

get_mountlocation ()
{
	LOCATION=$(mount | grep ${DEVICE_NODE#/dev/})
}

get_mountoptions ()
{
	MOUNT_OPTIONS=$(yad --form \
		--title="$TITLE" \
		--dialog-sep \
		--image=$IMAGE \
		--text="Select device to mount:" \
		--field="Device:":CB ${DEVICES//[[:space:]]/\!} \
		--field="Make Writeable (mount read/write)":CHK FALSE \
		--button="Device Information":2 \
		--button=gtk-cancel:1 \
		--button=gtk-ok:0)
	BUTTON=$?
}

mount_device ()
{
	if [ ! -e /media/${DEVICE_NODE#/dev/} ]; then
		sudo mkdir /media/${DEVICE_NODE#/dev/}
	fi
	sudo mount $OPTIONS $DEVICE_NODE /media/${DEVICE_NODE#/dev/}
	EXIT_CODE=$?
}

	mount_success ()
{
	get_mountlocation
	yad --title="$TITLE" \
		--image=gtk-ok \
		--dialog-sep \
		--text="\nDevice successfully mounted <b>$WRITE_TYPE</b> at: \n$LOCATION." \
		--button=gtk-ok
}

mount_fail ()
{
	yad --title="$TITLE" \
		--image=gtk-dialog-error \
		--dialog-sep \
		--text="\nDevice failed to mount.\nTry again, or mount manually." \
		--button=gtk-ok
}

print_deviceinfo ()
{
	DEVICEINFO=$(sudo blkid -o list)
	sudo fdisk -lu | yad --text-info --title="$TITLE" \
		--image=gtk-dialog-info \
		--dialog-sep \
		--text="<tt>${DEVICEINFO//<swap>/swap  }</tt>\n\nAddtional Disk Information:" \
		--height=400 \
		--button=gtk-ok
}

write_warning ()
{
	yad --title="$TITLE" \
		--image=gtk-dialog-warning \
		--dialog-sep \
		--text="This operation will allow writing to the device\nand <u>potentially destroy data</u>.\n\nDo you want to continue?" \
		--button=gtk-no:1 \
		--button=gtk-yes:0

	[[ $? = 1 ]] && exit 0
}

## Main application
MESSAGE="Operation requires administrative privileges.
Enter root password to continue:"
gksudo -m "$MESSAGE" echo >/dev/null

DEVICES=$(sudo blkid -o device)

get_mountoptions

LOOP=0
until [ $LOOP = 1 ]; do
	case $BUTTON in 
		0) LOOP=1 ;;
		1) exit 0 ;;
		2) print_deviceinfo; get_mountoptions ;;
	esac
done

for VAR in DEVICE_NODE WRITE_OPTION; do
	eval $VAR="\${MOUNT_OPTIONS%%|*}"
	MOUNT_OPTIONS="${MOUNT_OPTIONS#*|}"
done

if [ $WRITE_OPTION = TRUE ]; then
	WRITE_TYPE=read-write
else
	WRITE_TYPE=read-only
fi

get_mountlocation
#mount | grep ${DEVICE_NODE#/dev/}
if [ $? = 0 ]; then
	#get_mountlocation
	REMOUNT=$(yad --title="$TITLE" \
		--image=gtk-dialog-question \
		--dialog-sep \
		--text="The partition is already mounted:\n${LOCATION}\n\nDo you want to remount <u>$WRITE_TYPE</u> or unmount?" \
		--button=Remount:2 \
		--button=Unmount:0 \
		--button=gtk-cancel:1)
	case $? in
		0) sudo umount /media/${DEVICE_NODE#/dev/}; exit 0 ;;
		1) exit 0 ;;
		2) REMOUNT="remount," ;;
	esac
fi

case $WRITE_OPTION in
	FALSE) OPTIONS="-o ${REMOUNT}loop,ro,loop,noauto,user,noexec,nodev,noatime"
		mount_device
		check_mount ;;
	TRUE) OPTIONS="-o ${REMOUNT}rw"
		mount_device
		check_mount ;;
esac
